package com.a.lib_bean

import com.a.lib_bean.bean.PermissionBean
import com.a.lib_bean.enums.bPermissionEnum
import com.a.lib_bean.enums.bdPermissionEnum
import com.a.lib_bean.enums.PermissionEnum
import com.a.lib_bean.enums.PermissionType

object PermissionUtil {


    /*用这种写法，为了不让外部随意添加*/
    private val _permissionSet: LinkedHashSet<PermissionBean> = linkedSetOf()//非线程安全

    @JvmStatic
    val permissionSet: Set<PermissionBean>
        get() = _permissionSet

    private lateinit var type: PermissionType

    @JvmStatic
    fun initType(type: PermissionType) {
        PermissionUtil.type = type
    }
    // TODO: 2022/7/25 现在用的type判断耦合度高
    /**
     * 储存用户拥有的权限
     * */
    @JvmStatic
    fun addAll(permissionIDs: List<Int>) {
        val map: Map<Int, PermissionBean> =
            when (type) {
                PermissionType.c -> PermissionEnum.get()
                PermissionType.d -> bdPermissionEnum.get()
                else -> bPermissionEnum.get()
            }
        for (permissionID in permissionIDs) {
            map[permissionID]?.let { _permissionSet.add(it) }
        }
    }

    @JvmStatic
    fun clear() {
        if (_permissionSet.size > 0) _permissionSet.clear()
    }


    @JvmStatic
    fun getAllPermissionCode(): String {
        return when (type) {
            PermissionType.c -> PermissionEnum.getAllPermissionCode()
            PermissionType.d -> bdPermissionEnum.getAllPermissionCode()
            else -> bPermissionEnum.getAllPermissionBeanCode()
        }

    }


}